package com.it.tydic.estate.module.service.impl;

import com.it.tydic.estate.common.util.CurrentUtil;
import com.it.tydic.estate.common.util.InstaIdGenerator;
import com.it.tydic.estate.common.util.TimeUtil;
import com.it.tydic.estate.module.mapper.inner.TbGroupMapper;
import com.it.tydic.estate.module.mapper.inner.TbResourceMapper;
import com.it.tydic.estate.module.mapper.inner.TbRoleResourceMapper;
import com.it.tydic.estate.module.mapper.inner.TbUserGroupMapper;
import com.it.tydic.estate.module.model.inner.*;
import com.it.tydic.estate.module.service.GroupService;
import com.it.tydic.estate.module.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.*;

@Service
@Transactional
public class GroupServiceImpl implements GroupService {

    @Autowired
    private TbGroupMapper tbGroupMapper;

    @Autowired
    private TbUserGroupMapper tbUserGroupMapper;

    @Override
    public int deleteById(String id) {
        return tbGroupMapper.deleteByPrimaryKey(id);
    }

    @Override
    public TbGroup findById(String id) {
        return tbGroupMapper.selectByPrimaryKey(id);
    }


    @Override
    public int deleteByIds(List<String> idList) {
        Example example = new Example(TbGroup.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("groupId", idList);
        return tbGroupMapper.deleteByExample(example);
    }

    @Override
    public int addOrUpdate(TbGroup tbGroup) {
        int index=0;
        String userId= CurrentUtil.getCurrentUserId();
        if(StringUtil.isEmpty(tbGroup.getGroupId())){
            InstaIdGenerator idGenerator = new InstaIdGenerator();
            tbGroup.setGroupId(idGenerator.nextId(TimeUtil.timeGen())+"");
            tbGroup.setCreateTime(new Date());
            tbGroup.setCreateUser(userId);
            tbGroup.setGroupStatus(0);
            index= tbGroupMapper.insert(tbGroup);
        }else{
            tbGroup.setUpdateTime(new Date());
            tbGroup.setUpdateUser(userId);
            index=  tbGroupMapper.updateByPrimaryKeySelective(tbGroup);
        }
        return index;
    }


    public List<TbGroup> findGroupListByGroupId(String groupId) {
        return tbGroupMapper.selectAll();
    }
    @Override
    public List<TbGroup> findGroupTreeListByGroupId(String groupId) {
        List<TbGroup> groups=findGroupListByGroupId(groupId);
        if(groups.size() > 0){
            return toTree(groups);
        }
        return Collections.emptyList();
    }

    @Override
    public boolean groupIsUseByList(List<String> idList) {
        Example example = new Example(TbUserGroup.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("groupId", idList);
        int count=countGroupByExample(example);
        if(count > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean groupIsUseById(String id) {
        Example example = new Example(TbUserGroup.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("groupId", id);
        int count=countGroupByExample(example);
        if(count > 0){
            return true;
        }
        return false;
    }
    public int countGroupByExample(Example example){
        return tbUserGroupMapper.selectCountByExample(example);
    }

    private List<TbGroup> toTree(List<TbGroup> menu) {
        Map<String, TbGroup> temp = new LinkedHashMap<>();
        for (TbGroup r : menu) {
            temp.put(r.getGroupId(), r);
        }
        List<TbGroup> result = new ArrayList<TbGroup>();
        for (TbGroup child : menu) {
            String parentId = child.getParentId();
            if (parentId.equals(String.valueOf(0))) {
                result.add(child);
                continue;
            }
            TbGroup parent = temp.get(parentId);
            if(parent != null){
                parent.getChildren().add(child);
            }
        }
        return result;
    }
}
